package com.gitee.luosl.cola.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {

    /**
     * 根据正则 Pattern 抽取匹配到得条目
     * Pattern pattern = Pattern.compile("([0-9]{4})-([0-9]{2})-([0-9]{2})")
     * String input = "2020-10-11";
     * extractGroups(pattern, input)
     * ["2020", "10", "11"]
     * @param pattern Pattern
     * @param input input
     * @return List[string]
     */
    public static List<String> extractGroups(Pattern pattern, CharSequence input){
        Matcher matcher = pattern.matcher(input);
        if(matcher.matches()){
            int cnt = matcher.groupCount();
            List<String> collector = new ArrayList<>();
            for(int i =1; i<=cnt; i++){
                collector.add(matcher.group(i));
            }
            return collector;
        }
        return new ArrayList<>();
    }


    public static List<String> extractGroups(String regex, CharSequence input) {
        return extractGroups(Pattern.compile(regex), input);
    }

    public static boolean isIntStr(String str){

        int result = 0;
        int radix = 10;
        int i = 0, len = str.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = str.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+')
                    return false;

                if (len == 1) // Cannot have lone "+" or "-"
                    return false;
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(str.charAt(i++),radix);
                if (digit < 0) {
                    return false;
                }
                if (result < multmin) {
                    return false;
                }
                result *= radix;
                if (result < limit + digit) {
                    return false;
                }
                result -= digit;
            }
        } else {
            return false;
        }

        return true;
    }

    public static boolean isLongStr(String str) {
        int radix = 10;
        long result = 0;
        boolean negative = false;
        int i = 0, len = str.length();
        long limit = -Long.MAX_VALUE;
        long multmin;
        int digit;

        if (len > 0) {
            char firstChar = str.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Long.MIN_VALUE;
                } else if (firstChar != '+')
                    return false;

                if (len == 1) // Cannot have lone "+" or "-"
                    return false;
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(str.charAt(i++),radix);
                if (digit < 0) {
                    return false;
                }
                if (result < multmin) {
                    return false;
                }
                result *= radix;
                if (result < limit + digit) {
                    return false;
                }
                result -= digit;
            }
        } else {
            return false;
        }
        return true;
    }


}
